<?php

namespace app_supplier\models;

use Yii;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "{{%auth_account}}".
 *
 * @property integer $id
 * @property integer $hq_id
 * @property integer $store_id
 * @property string $account
 * @property string $password
 * @property string $pwd
 * @property string $name
 * @property integer $reg_time
 * @property integer $last_login_time
 * @property integer $status
 * @property integer $is_sys_manager
 * @property string $remark
 */
class AuthAccount extends ActiveRecord
{
    private $_user = null;
    public $account = '';
    public $password = '';
    public $rememberMe = true;
    public $error = null;
    private static $_time = 3600 * 24 * 30 ;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%auth_account}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['account', 'required', 'message' => '用户名不可以为空'],
            ['password', 'required', 'message' => '密码不可以为空'],
            ['rememberMe', 'boolean'],
            [['account', 'name'], 'string', 'max' => 20],
            [['password', 'pwd'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'hq_id' => 'Hq ID',
            'store_id' => 'Store ID',
            'account' => '手机号码',
            'password' => '密码',
            'pwd' => 'Pwd',
            'name' => 'Name',
            'reg_time' => 'Reg Time',
            'last_login_time' => 'Last Login Time',
            'status' => 'Status',
            'is_sys_manager' => 'Is Sys Manager',
            'remark' => 'Remark',
            'rememberMe'=>''
        ];
    }
    public function login()
    {
        if ($this->validate() && $this->getUser()) {
            if ($this->validatePassword('password')) {
                $login =  Yii::$app->user->login($this->getUser(), $this->rememberMe ? self::$_time : 0);
                if ($login) {
                    if ($row = User::findUserInfo($this->account, $this->encryptMd5Password())) {
                        Yii::$app->user->setIdentity($this->getUser());
                        return true;
                    } else {
                        $this->error = '此用户不是供应商用户';
                        Yii::$app->user->logout();
                        return false;
                    }
                } else {
                    $this->error = '登录失败--请联系管理员';
                    return false;
                }
            } else {
                $this->error = '密码错误';
                return false;
            }
        } else {
            $this->addError('account', '用户不存在！');
            $this->error='用户不存在';
            return false;
        }
    }

    public function validatePassword($attribute)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, '密码错误');
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    private function getUser()
    {
        if ($this->_user === null) {
            $this->_user = User::findByUsername($this->account);
        }
        return $this->_user;
    }
    public function encryptMd5Password()
    {
        return md5($this->password);
    }
}
